package com.duleme.servlet.order;

import com.duleme.common.EncodeUtils;
import com.duleme.common.JsonUtils;
import com.duleme.entity.Order;
import com.duleme.service.ShopService;
import com.duleme.service.impl.ShopServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "SetOrderServlet",urlPatterns = "/data/order/set")
public class SetOrderServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        int orderId = Integer.valueOf(request.getParameter("orderId"));
        int orderState = Integer.valueOf(request.getParameter("orderState"));
        ShopService shopService = new ShopServiceImpl();
        Order order = shopService.getOrderByID(orderId);
        order.setOrderState(orderState);
        PrintWriter writer = response.getWriter();
        Map<String,Integer> map = new HashMap<>();
        if(shopService.setOrderState(order)){
            map.put("result",1);
            String json = JsonUtils.object2Json(map);
            writer.write(json);
            System.out.println("订单发货成功"+json);
            writer.close();
        }else {
            map.put("result",2);
            String json = JsonUtils.object2Json(map);
            writer.write(json);
            System.out.println("订单发货失败"+json);
            writer.close();
        }

    }
}
